/*
 * GraficoBarras.cpp
 *
 *  Created on: 09/06/2012
 *      Author: sebastian
 */

#include "GraficoBarras.h"
#include "gtkmm-2.4/gtkmm.h"
#include <iostream>
#include <string>

GraficoBarras::GraficoBarras() {
	maxValue = 0;
}

GraficoBarras::~GraficoBarras() {
}
/*
int main(int argc, char** argv) {
	Gtk::Main kit(argc, argv);
	Gtk::Window win;
	win.set_title("Grafico de Barras");

	GraficoBarras barGraph;

	std::list<unsigned int> valores;
	valores.push_back(500);
	valores.push_back(700);
	valores.push_back(900);
	valores.push_back(1100);

	std::list<unsigned int> valores2;
	valores2.push_back(1300);
	valores2.push_back(1700);
	valores2.push_back(2100);
	valores2.push_back(1300);

	std::list<unsigned int> valores3;
	valores3.push_back(1200);
	valores3.push_back(1800);
	valores3.push_back(2400);
	valores3.push_back(1700);

	std::list<unsigned int> valores4;
	valores4.push_back(1600);
	valores4.push_back(1700);
	valores4.push_back(2000);
	valores4.push_back(2390);

	std::list<unsigned int> valores5;
	valores5.push_back(1300);
	valores5.push_back(1400);
	valores5.push_back(2600);
	valores5.push_back(3100);

	std::list<unsigned int> valores6;
	valores6.push_back(1300);
	valores6.push_back(1500);
	valores6.push_back(1700);
	valores6.push_back(1050);

	barGraph.agregarNombre("Ventas");
	barGraph.agregarNombre("Compras");
	barGraph.agregarNombre("Saldo");
	barGraph.agregarNombre("Gastos");
	barGraph.agregarBarras(valores, "Marcelo");
	barGraph.agregarBarras(valores2, "Monica");
	barGraph.agregarBarras(valores3, "Mateo");
	barGraph.agregarBarras(valores4, "Marta");
	barGraph.agregarBarras(valores5, "Maria");
	barGraph.agregarBarras(valores6, "Mariano");


	win.add(barGraph);
	barGraph.show();
	Gtk::Main::run(win);
	return 0;
}
*/
bool GraficoBarras::on_expose_event(GdkEventExpose* event) {
	Glib::RefPtr<Gdk::Window> win = get_window();
	if(win) {
		Gtk::Allocation alloc = get_allocation();

		int width = alloc.get_width();
		int height = alloc.get_height();
		int lesser = width;
		if(height < width)
			lesser = height;
		Cairo::RefPtr<Cairo::Context> cr = win->create_cairo_context();


		float font_size = lesser / 30;

		//cr->rectangle(0, 0, maxSize *1.3*font_size, valores.size() *font_size * 1.6);
		//cr->stroke();
		std::list<barras*>::iterator it;
		unsigned int i = 0;
		for(it = conjuntosBarras.begin(); it != conjuntosBarras.end(); ++it) {
			dibujarBarras(cr, *it, height, width, i, conjuntosBarras.size());
			++i;
			std::cout << conjuntosBarras.size() << std::endl;
		}

		cr->set_line_width(font_size / 4);
		cr->set_source_rgb(0, 0, 0);
		cr->rectangle(width * 0.03, height * 0.03, width * 0.93, height * 0.93);
		cr->stroke();
	}
	return true;
}

void GraficoBarras::agregarBarras(std::list<unsigned int> valores, std::string nombre) {
	barras* bar = new barras;
	bar->valores = valores;
	std::list<unsigned int>::iterator it;
	for(it = valores.begin(); it != valores.end(); ++it)
		if((*it) > maxValue)
			maxValue = (*it);
	bar->nombre = nombre;
	conjuntosBarras.push_back(bar);
}

void GraficoBarras::dibujarBarras(const Cairo::RefPtr<Cairo::Context>& cr, barras* bars, unsigned int height, unsigned int width, unsigned int current, unsigned int total) {
	unsigned int cant = (bars->valores).size();
	//unsigned int total = conjuntosBarras.size();
	float barSize = ((width / total) / cant) / 1.4;
	cr->set_source_rgb(1, 0, 0);
	cr->set_line_width(barSize);
	float corrimientoInicial = width*0.1;
	float corrimientoPorNumeroConjunto = barSize * cant * current * 1.2;
	cr->move_to(corrimientoInicial + corrimientoPorNumeroConjunto, height * 0.8);
	std::list<unsigned int>::iterator it;
	unsigned int j = 0;
	float fontSize = barSize / 5;

	cr->save();
	cr->move_to(width*BARS_X_OFFSET_FACTOR + corrimientoPorNumeroConjunto, height * BARS_Y_OFFSET_FACTOR + 1.5 * fontSize);
	cr->set_source_rgb(0, 0, 0);
	cr->set_font_size(fontSize * 1.4);
	cr->show_text(bars->nombre);
	cr->stroke();
	cr->restore();

	cr->move_to(width*BARS_X_OFFSET_FACTOR + corrimientoPorNumeroConjunto, height * BARS_Y_OFFSET_FACTOR);


	for(it = bars->valores.begin(); it != bars->valores.end(); ++it) {
		float rojo, verde, azul;

		rojo = (j % 2) * 0.6;
		verde = (j % 2) * 0.5;
		azul = (j % 3) * 0.6;

		cr->set_source_rgb(rojo, verde, azul);
		cr->rel_line_to(0,-(height / maxValue * 0.6) * (*it));
		cr->stroke();


		float corrimientoPorCantBarras = barSize * j;


		cr->save();
		cr->move_to(width*BARS_X_OFFSET_FACTOR + corrimientoPorNumeroConjunto + corrimientoPorCantBarras - 0.1 * fontSize, height * BARS_Y_OFFSET_FACTOR + 3 * fontSize);
		cr->rotate(M_PI / 2);
		cr->set_font_size(fontSize * 1);
		cr->show_text(nombresBarras[j]);
		cr->stroke();
		cr->restore();

		++j;
		corrimientoPorCantBarras = barSize * j;
		cr->move_to(width*BARS_X_OFFSET_FACTOR + corrimientoPorNumeroConjunto + corrimientoPorCantBarras, height * BARS_Y_OFFSET_FACTOR);
	}
}

void GraficoBarras::agregarNombre(std::string nomb) {
	nombresBarras.push_back(nomb);
}
